<?php
// 加入购物车
require_once $this->website['path']['action'] . '/app/token.php';
$uid = $basic['uid'];

$json = array('status' => 'error', 'msg' => 'error', 'data' => null);

$id = fn_post('id');
$param = fn_post('param');
$price = fn_post('price');
$num = fn_post('num');
$sid = fn_post('sid');

if (!$this->website['class']['db']
	->table('product')
	->where('id=? and checked=1', $id)
	->limit(1)
	->count()) {
	$json['msg'] = '未找到商品或已下架';
} elseif ($this->website['class']['db']
		->table('product_cart')
		->where('pid=? and uid=? and sid=? and price=? and param=?', $id, $uid, $sid, $price, $param)
		->limit(1)
		->count()) {
	$this->website['class']['db']
		->table('product_cart')
		->field('num')
		->value($num)
		->where('pid=? and uid=? and sid=? and price=? and param=?', $id, $uid, $sid, $price, $param)
		->limit(1)
		->increase();
	$json['status'] = "ok";
	$json['msg'] = "加入成功";
} else {
	$cid = $this->website['class']['db']
		->table('product_cart')
		->field('pid', 'sid', 'uid', 'price', 'num', 'createtime', 'visbile', 'param')
		->value($id, $sid, $uid, $price, $num, $_SERVER['REQUEST_TIME'], 1, $param)
		->insert();
	$json['status'] = "ok";
	$json['msg'] = "加入成功";
}
$this->json_encode_encrypt($json);